package com.example.parkingproject.service.serviceImp;

import com.example.parkingproject.code.ServiceCode;
import com.example.parkingproject.ex.ServiceException;
import com.example.parkingproject.jopo.entity.Allega;
import com.example.parkingproject.mapper.AllegaMapper;
import com.example.parkingproject.service.AllegaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class AllegaServiceImp implements AllegaService {
    @Autowired
    AllegaMapper allegaMapper;

    @Override
    public void newAllega(Allega allega) {
        allega.setCreateTime(new Date());

        Integer getCar =  allegaMapper.getcar(allega.getCarId());
        if(getCar==0){
            throw new ServiceException(ServiceCode.error_bad_request,"车辆信息不存在");
        }
        allegaMapper.newAllega(allega);
    }

    @Override
    public List<Allega> loadAllega(String cardId) {
        List<Allega> list = allegaMapper.loadAllega(cardId);

        return list;

    }

    @Override
    public void delete(Integer id) {
        allegaMapper.delete(id);
    }

    @Override
    public List<Allega> handleLoadAllega(String cardId) {
        List<Allega> list = allegaMapper.handleLoadAllega(cardId);
        return list;
    }

    @Override
    public Allega getAllage(Integer id) {

        Allega allega = allegaMapper.getAllage(id);

        if (allega == null) {
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, "查看数据不存在");
        }
        return allega;
    }

    @Override
    public List<Allega> adminLoadAllega() {
        List<Allega> list = allegaMapper.adminLoadAllega();
        return list;
    }

    @Override
    public void handle(Allega allega) {
        allega.setHandleTime(new Date());
        allegaMapper.handle(allega);
        log.debug("黑名单参数{}",allega);
        Integer row =   allegaMapper.getBlickList(allega.getCarId());
        log.debug("参数{}",row);
        if (row>=3){
            allega.setCreateTime(new Date());
            allegaMapper.addBlickList(allega);
        }

    }

    @Override
    public Allega userGetAllage(Integer id) {
        Allega allega = allegaMapper.userGetAllage(id);
        if (allega==null){
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,"尝试获取的数据不存在请核对");
        }
        return allega;
    }

    @Override
    public void update(Allega allega) {
        Allega allage = allegaMapper.userGetAllage(allega.getId());
        log.debug("修改参数{}",allage);
        if (allage==null){
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,"尝试修改的数据不存在");
        }
        allegaMapper.update(allega);
    }

    @Override
    public Integer getHistory() {
        Integer count =  allegaMapper.getHistory();
        return count;
    }

    @Override
    public List<Integer> groupAllega() {
        List<Integer> list = allegaMapper.groupAllega();
        return list;
    }

    @Override
    public List<Allega> adminHandleLoadAllega() {

        List<Allega> list = allegaMapper.adminHandleLoadAllega();
        return list;
    }
}
